﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.views.components {
    import flash.events.*;
    import flash.display.*;
    import flash.geom.*;
    import flash.text.*;
    import flash.utils.*;
    import net.wooga.pets.events.*;
    import net.wooga.pets.constants.*;
    import net.wooga.pets.utils.*;

    public class RewardsDisplay extends Sprite {

        private var _rewardStartPos:Point;
        private var _rewardLandingXStart:int;
        private var _rewardLandingXEnd:int;
        private var _rewardBackgroundAnimation:MovieClip;
        private var _iconsToAnimateStack:Array;

        public function RewardsDisplay(){
            l(("RewardsDisplay: " + RewardsDisplay));
            this._iconsToAnimateStack = new Array();
            this.createChildren();
        }
        public function createChildren():void{
            this._rewardBackgroundAnimation = AssetsLibrary.getMovieClip("RewardAnimationBackground");
            this._rewardBackgroundAnimation.stop();
            this._rewardBackgroundAnimation.visible = false;
            addChild(this._rewardBackgroundAnimation);
        }
        public function addRewardItem(_arg1:String, _arg2:String, _arg3:String, _arg4:Boolean):void{
            var _local5:MovieClip;
            var _local7:int;
            var _local9:TextFormat;
            if (getChildByName(_arg1)){
                return;
            };
            _local5 = AssetsLibrary.getMovieClip("RewardAnimation");
            _local5.mouseChildren = false;
            _local5.mouseEnabled = true;
            _local5.buttonMode = true;
            _local5.stop();
            var _local6:int = this._rewardStartPos.x;
            _local7 = Numbers.randomNumberBetween(this._rewardLandingXStart, this._rewardLandingXEnd);
            if (!_arg4){
                _local5.gotoAndStop("end");
                _local5.x = _local7;
            } else {
                _local5.x = (_local5.startX = _local6);
                _local5.endX = _local7;
                _local5.addEventListener(Event.ENTER_FRAME, this.onAnimation);
                _local5.gotoAndPlay(1);
                this._rewardBackgroundAnimation.visible = true;
                this._rewardBackgroundAnimation.gotoAndPlay(1);
            };
            var _local8:MovieClip = AssetsLibrary.getMovieClip(_arg2);
            if (_local8){
                _local9 = new TextFormat();
                _local9.font = "ShowcardGothic";
                if (_local8.textTween){
                    _local8.textTween.rewardText.tf.embedFonts = true;
                    _local8.textTween.rewardText.tf.htmlText = _arg3;
                    _local8.textTween.rewardText.tf.autoSize = TextFieldAutoSize.CENTER;
                    _local8.textTween.rewardText.tf.multiline = false;
                    _local8.textTween.rewardText.tf.setTextFormat(_local9);
                    _local8.textTween.visible = false;
                };
                _local5.name = _arg1;
                _local5.y = this._rewardStartPos.y;
                _local5.addEventListener(MouseEvent.CLICK, this.onRewardItemClicked);
                _local5.addEventListener(MouseEvent.ROLL_OVER, this.onRewardItemOver);
                _local5.addEventListener(MouseEvent.ROLL_OUT, this.onRewardItemOut);
                _local5.itemContainer.addChild(_local8);
                addChild(_local5);
            };
        }
        public function collectRewardItem(_arg1:String):void{
            var rewardIcon:MovieClip;
            var item:MovieClip;
            var id = _arg1;
            rewardIcon = MovieClip(getChildByName(id));
            if (rewardIcon){
                rewardIcon.stop();
                rewardIcon.removeEventListener(MouseEvent.CLICK, this.onRewardItemClicked);
                rewardIcon.removeEventListener(MouseEvent.ROLL_OVER, this.onRewardItemOver);
                rewardIcon.removeEventListener(MouseEvent.ROLL_OUT, this.onRewardItemOut);
                rewardIcon.mouseChildren = false;
                rewardIcon.mouseEnabled = false;
                rewardIcon.buttonMode = false;
                if (((((rewardIcon) && (rewardIcon.itemContainer))) && (rewardIcon.itemContainer.getChildAt(0)))){
                    item = (rewardIcon.itemContainer.getChildAt(0) as MovieClip);
                    item.gotoAndStop("selected");
                    item.textTween.visible = true;
                    item.textTween.gotoAndPlay("start");
                };
                setTimeout(function ():void{
                    Displays.removeChild(rewardIcon);
                }, 1000);
            };
        }
        private function onAnimation(_arg1:Event):void{
            var _local2:MovieClip = (_arg1.currentTarget as MovieClip);
            if (_local2.currentFrameLabel == "end"){
                _local2.removeEventListener(Event.ENTER_FRAME, this.onAnimation);
                _local2.x = _local2.endX;
                this._rewardBackgroundAnimation.visible = false;
            };
            _local2.x = (_local2.x + ((_local2.endX - _local2.startX) / _local2.framesLoaded));
        }
        private function onRewardItemOut(_arg1:MouseEvent):void{
            this.gotoFrame(_arg1, "default");
        }
        private function onRewardItemOver(_arg1:MouseEvent):void{
            this.gotoFrame(_arg1, "hover");
        }
        private function gotoFrame(_arg1:MouseEvent, _arg2:String):void{
            var _local3:MovieClip = (_arg1.currentTarget as MovieClip);
            if (((((_local3) && (_local3.itemContainer))) && (_local3.itemContainer.getChildAt(0)))){
                MovieClip(_local3.itemContainer.getChildAt(0)).gotoAndStop(_arg2);
            };
        }
        private function onRewardItemClicked(_arg1:MouseEvent):void{
            _arg1.stopPropagation();
            var _local2:String = _arg1.currentTarget.name;
            log(("onRewardItemClicked: " + _local2));
            dispatchEvent(new InfoEvent(EventConstants.REWARD_ICON_CLICKED, _local2));
        }
        public function setRewardPosition(_arg1:Point, _arg2:int, _arg3:int):void{
            this._rewardStartPos = _arg1;
            this._rewardLandingXStart = _arg2;
            this._rewardLandingXEnd = _arg3;
        }
        public function setBackgroundAnimationPosition(_arg1:Point):void{
            this._rewardBackgroundAnimation.x = _arg1.x;
            this._rewardBackgroundAnimation.y = _arg1.y;
        }

    }
}//package net.wooga.pets.views.components 
